<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.mselect.js?"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/validator.js"></script>
<script type="text/javascript">
$(function(){
	var shipping_method_data = <?=$shipping_method_data?>;
	$(document).mselect(shipping_method_data, 'shipping_method_id', 'shipping_method_id', "<?=isset($sales_order['shipping_method']) ? $sales_order['shipping_method'] : 0?>", 'shipping_method_id');
	$("#sale_time").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	$("#send_method").change(function()
	{
		if ($(this).val()==<?=Q::ini('custom_flag/sales_order_send_method/resend/value')?>)
		{
			$("#resend_reason, #resend_sales_order_id").show();
		}
		else
		{
			$("#resend_reason, #resend_sales_order_id").hide();
		}
	});
	var i = 0 ;
	var attr_count = 0;
	$("#add_product").click(function(){
		var _html = "<tr>";
		_html += "<td><input type='checkbox' name='item[" + i + "][gift_flag]' value='<?=Q::ini('custom_flag/sales_order_item_gift_flag/gift/value')?>' /></td>";
		_html += "<td><input type='text' size='7' name='item[" + i + "][product_basic_number]' /></td>";
		_html += "<td><input type='text' size='7' name='item[" + i + "][sales_price]' value='' /></td>";
		_html += "<td><input type='text' size='7' name='item[" + i + "][quantity]' value='' /></td>";
		_html += "<td><input type='text' name='item[" + i + "][shop_item_id]' value='' />";
		_html += "<td><input type='text' name='item[" + i + "][shop_transaction_id]' value='' />";
		_html += "<td><a href='javascript:void(0);' class='add_attr' rel='item["+ i +"][attr]'>添加属性</a> <a href='javascript:void(0);' class='del_product'>删除产品</a></td>";
		_html += "</tr>";
		$("#list").append(_html);
		i++;
	});
	$(".add_attr").live("click", function(){
		var unid = $(this).attr("rel");
		var _html = "<tr>";
		_html += "<td><input type='text' name='"+unid+"["+attr_count+"][name]' value='' /></td>";
		_html += "<td><input type='text' name='"+unid+"["+attr_count+"][value]' value='' /></td>";
		_html += "<td><select name='"+unid+"["+attr_count+"][prefix]'><option value='+'>+</option><option value='-'>-</option></select></td>";
		_html += "<td><input type='text' size='7' name='"+unid+"["+attr_count+"][price]' value='' /></td>";
		_html += "<td><a href='javascript:void(0);' class='del_attr'>删除属性</a></td></tr>";
		attr_count++;
		if ($(this).parent().parent().next().is(".attr"))
		{
			$(this).parent().parent().next().find("table").append(_html);
		}
		else
		{
			_html = "<tr class='attr'><td colspan='7'><table width='100%' ><tr><td>属性名称</td><td>属性值</td><td width='100'>属性价格前缀</td><td width='70'>属性价格</td><td>属性操作</td></tr>" + _html + "</table>";
			$(this).parent().parent().after(_html);
		}
	});
	$(".del_attr").live("click", function(){
		if ( ! confirm("确定删除该属性？"))
		{
			return false;
		}
		else
		{
			$(this).parent().parent().remove();
		}
	});
	$(".del_product").live("click", function(){
		if ( ! confirm("确定删除该产品？"))
		{
			return false;
		}
		else
		{
			if($(this).parent().parent().next().is(".attr"))
			{
				$(this).parent().parent().next().remove();
			}
			$(this).parent().parent().remove();
		}
	});
	$("#send_method").change();
	$("form").submit(function(){  
		var flag = Validator.Validate(this,3);
		if (flag)
		{
			 $("#submit").attr("disabled","disabled");  
		}
		return flag;
	}); 
});
</script>
<h2>新增销售订单</h2>
<form action="<?=url('Sales::Order/CreateSave')?>" method="post">
<table width="80%" class="form_table">
	<tr>
		<th>网站记录号</th>
		<td><input type="text" name="shop_order_number" value="<?=isset($sales_order) ? 0 : ''?>" /></td>
		<th>网站/店铺</th>
		<td>
			<select id="shop_id" name="shop_id" dataType="Require" msg="请选择">
				<option value="">请选择..</option>
				<?foreach ($shop as $k => $sh):?>
				<optgroup label="<?=$k?>">
				<?foreach ($sh as $s):?>
				<option value="<?=$s['id']?>" <?if(isset($sales_order)&&$sales_order['shop_id'] == $s['id']) echo "selected='selected'";?>><?=$s['name']?></option>
				<?endforeach;?>
				</optgroup>
				<?endforeach;?>
			</select>
			<span class="red">*</span>
		</td>
	</tr>
	<tr>
		<th>支付方式</th>
		<td>
			<select name="payment_method" dataType="Require" msg="请选择">
				<option value="">请选择</option>
				<?foreach (Q::ini('custom_flag/sales_order_payment_method') as $pm):?>
				<option value="<?=$pm['value']?>" <?if(isset($sales_order)&&$sales_order['payment_method']==$pm['value']) echo "selected='selected'";?>><?=$pm['name']?></option>
				<?endforeach;?>
			</select>
			<span class="red">*</span>
		</td>
		<th>发货类型</th>
		<td>
			<select name="send_method" id="send_method" dataType="Require" msg="请选择">
				<option value="">请选择</option>
				<?foreach (Q::ini('custom_flag/sales_order_send_method') as $sm):?>
				<option value="<?=$sm['value']?>" <?if(isset($sales_order)&&$sm['value']==Q::ini('custom_flag/sales_order_send_method/resend/value')) echo "selected='selected'"?>><?=$sm['name']?></option>
				<?endforeach;?>
			</select>
			<select name="resend_reason" id="resend_reason" style="display:none;">
			<?foreach (Q::ini('custom_flag/sales_order_resend_reason') as $rr):?>
			<option value="<?=$rr['value']?>"><?=$rr['name']?></option>
			<?endforeach;?>
			</select>
			<input type="text" size="13" id="resend_sales_order_id" style="display:none;" name="resend_sales_order_id" value="<?=isset($sales_order)?$sales_order['number']:''?>"  />
			<span class="red">*</span>
		</td>
	</tr>
	<tr>
		<th>仓库/邮寄</th>
		<td>
			<input type="hidden" name="shipping_method_id" id="shipping_method_id" value=""  dataType="Require" msg="请选择" />
			<span class="red">*</span>
		</td>
		<th>销售人员</th>
		<td>
			<select name="sales_user_id">
					<? foreach($user as $u):?>
					<option value="<?=$u['id']?>" <?=(CURRENT_USER_ID == $u['id']) ? "selected='selected'" : ''?>><?=$u['name'];?></option>
					<? endforeach;?>
			</select>
		</td>
	</tr>
	<tr>
		<th>币种</th>
		<td>
			<select name="currency_id" dataType="Require" msg="请选择">
				<option value="">请选择..</option>
				<?foreach ($currency as $c):?>
				<option value="<?=$c['id']?>" <?if(isset($sales_order)&&$sales_order['currency_id']==$c['id'])echo "selected='selected'";?>><?=$c['code']?></option>
				<?endforeach;?>
			</select>
			<span class="red">*</span>
		</td>
		<th>销售日期</th>
		<td><input type="text" name="sales_time" id="sale_time" value="<?=isset($sales_order)?$sales_order['sales_time']:''?>" dataType="Require" msg="请填写"/> <span class="red">*</span></td>
	</tr>
	<tr>
		<th>交易号</th>
		<td><input type="text" name="transaction_id" value="<?=isset($sales_order) ? $sales_order['transaction_id'] : ''?>" /></td>
		<th>买家ID</th>
		<td><input type="text" name="shop_buyer_id" value="<?=isset($customer) ? $customer['shop_buyer_id'] : ''?>" /></td>
	</tr>
	<tr>
		<th>买家姓名</th>
		<td><input type="text" name="fullname" value="<?=isset($customer) ? htmlspecialchars($customer['fullname']) : ''?>" dataType="Require" msg="请填写"> <span class="red">*</span></td>
		<th>买家电话</th>
		<td><input type="text" name="phone" value="<?=isset($customer) ? $customer['phone']:''?>" /></td>
	</tr>
	<tr>
		<th>买家Email</th>
		<td><input type="text" name="email" value="<?=isset($customer) ? $customer['email']:''?>" /></td>
		<th>折扣</th>
		<td><input type="text" name="discount" /></td>
	</tr>
	<tr>
		<th>收货地址1</th>
		<td><input type="text" name="street1" value="<?=isset($customer) ? htmlspecialchars($customer['street1']) : ''?>" dataType="Require" msg="请填写 " /> <span class="red">*</span></td>
		<th>收货地址2</th>
		<td><input type="text" name="street2" value="<?=isset($customer) ? htmlspecialchars($customer['street2']) : ''?>" /></td>
	</tr>
	<tr>
		<th>所在城市</th>
		<td><input type="text" name="city" value="<?=isset($customer) ? htmlspecialchars($customer['city']) :''?>" dataType="Require" msg="请填写"/> <span class="red">*</span></td>
		<th>所在州/省</th>
		<td><input type="text" name="state" value="<?=isset($customer) ? htmlspecialchars($customer['state']) :''?>" dataType="Require" msg="请填写 "/> <span class="red">*</span></td>
	</tr>
	<tr>
		<th>所在国家</th>
		<td><input type="text" name="country" value="<?=isset($customer) ? htmlspecialchars($customer['country']) : ''?>" dataType="Require" msg="请填写 "/> <span class="red">*</span></td>
		<th>所在国家(中文)</th>
		<td><input type="text" name="country_cn" value="<?=isset($customer) ? htmlspecialchars($customer['country_cn']) : ''?>" dataType="Require" msg="请填写 "/> <span class="red">*</span></td>
	</tr>
	<tr>
		<th>邮政编码</th>
		<td><input type="text" name="zip" value="<?=isset($customer) ? $customer['zip'] : ''?>" dataType="Require" msg="请填写 "/> <span class="red">*</span></td>
		<th>运费</th>
		<td><input type="text" name="shipping_fee"/></td>
	</tr>
	<tr>
		<th>地址备注</th>
		<td colspan="3"><textarea rows="3" name="address_notes" cols="60"></textarea></td>
	</tr>
</table>
<table width="100%" class="list_table" id="list">
<tr>
	<th>赠品</th>
	<th>产品编号</th>
	<th>单价</th>
	<th>数量</th>
	<th>ShopItemId</th>
	<th>ShopTransactionId</th>
	<th width="120">操作</th>
</tr>
<?if (isset($sales_order['item'])):?>
<?foreach ($sales_order['item'] as $si):?>
<tr>
	<td><input type="checkbox" name="item[<?=$si['id']?>][gift_flag]" value="<?=$si['gift_flag'];?>"></td>
	<td><input type="text" size="7" name="item[<?=$si['id']?>][product_basic_number]" value="<?=$si['product_basic_number']?>" ></td>
	<td><input type="text" size="7" name="item[<?=$si['id']?>][sales_price]" value="<?=$si['sales_price']?>" ></td>
	<td><input type="text" size="7" name="item[<?=$si['id']?>][quantity]" value="<?=$si['quantity']?>" ></td>
	<td><input type="text" name="item[<?=$si['id']?>][shop_item_id]" value="<?=$si['shop_item_id']?>" ></td>
	<td><input type="text" name="item[<?=$si['id']?>][shop_transaction_id]" value="<?=$si['shop_transaction_id']?>" ></td>
	<td><a href="javascript:void(0);" class="add_attr" rel="item[<?=$si['id']?>][attr]">添加属性</a> <a href="javascript:void(0);" class="del_product">删除产品</a></td>
</tr>
<?if ($si['attr']):?>
<tr class="attr">
	<td colspan="7">
		<table width="100%">
			<tr>
				<td>属性名称</td>
				<td>属性值</td>
				<td width="100">属性价格前缀</td>
				<td width="70">属性价格</td>
				<td>属性操作</td>
			</tr>
			<?foreach ($si['attr'] as $sia):?>
			<tr>
				<td><input type="text" name="item[<?=$si['id']?>][attr][<?=$sia['id']?>][name]" value="<?=$sia['name'];?>"></td>
				<td><input type="text" name="item[<?=$si['id']?>][attr][<?=$sia['id']?>][value]" value="<?=$sia['value'];?>"></td>
				<td><select name="item[<?=$si['id']?>][attr][<?=$sia['id']?>][prefix]"><option value="+" <?if ($sia['prefix'] == '+') echo "checked='checked'"?>>+</option><option value="-" <?if ($sia['prefix'] == '-') echo "checked='checked'"?>>-</option></select></td>
				<td><input type="text" size="7" name="item[<?=$si['id']?>][attr][<?=$sia['id']?>][price]" value="<?=$sia['price']?>"></td>
				<td><a href="javascript:void(0);" class="del_attr">删除属性</a></td>
			</tr>
			<?endforeach;?>
		</table>
	</td>
</tr>
<?endif;?>
<?endforeach;?>
<?endif;?>
</table>
<a id="add_product">添加该订单产品</a>
<div align="center"><input id="submit" type="submit" class="button" value="添加订单" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/></div>
</form>
<?php $this->_endblock(); ?>